using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

using PHSRAG.Insight21.FormsManagement;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using Utility = PHSRAG.Utility;

namespace InsightMigrationUtility
{
    public class QuestionnaireComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(((PHSRAG.Insight21.FormsManagement.Questionnaire)x).Name, ((PHSRAG.Insight21.FormsManagement.Questionnaire)y).Name));
        }
    }

    public class Questionnaire : PHSRAG.Insight21.FormsManagement.Questionnaire
    {
        private DBAccess dbAccess;
        private ArrayList versions;

        public Questionnaire(ICachePolicy cachePolicy)
            : base(cachePolicy)
        {

        }

        public Questionnaire(ICachePolicy cachePolicy, DataRow dataRow, DBAccess dbAccess)
            : base(cachePolicy, dataRow)
        {
            this.dbAccess = dbAccess;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            PHSRAG.Insight21.FormsManagement.Questionnaire oQ = obj as PHSRAG.Insight21.FormsManagement.Questionnaire;
            if ((object)oQ == null)
            {
                return false;
            } 

            return (oQ.Name == Name) && (oQ.Type == Type);
        }

        public bool Equals(Questionnaire obj)
        {
            if ((object)obj == null)
            {
                return false;
            }

            return (obj.Name == Name) && (obj.Type == Type);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public void AddQuestionnaireVersion(QuestionnaireVersion newVersion, bool setActive)
        {
            int maxVersionNumber = 0;
            foreach (QuestionnaireVersion questionnaireVersion in Versions)
            {
                if (maxVersionNumber < questionnaireVersion.Version)
                    maxVersionNumber = questionnaireVersion.Version;
                if (setActive)
                    questionnaireVersion.IsActive = false;
            }
            newVersion.Version = maxVersionNumber + 1;
            newVersion.IsActive = setActive;

            Versions.Add(newVersion);
            // Have to set this because the SaveObject method checks the private member and that's not
            // set unless we call the set on the property.
            base.Versions = Versions;
            Dirty = true;
        }

        public override ArrayList Versions
        {
            get
            {
                if (versions == null)
                {
                    versions = new ArrayList();
                    DataSet ds = Utility.DataAccess.GetDataSet(dbAccess.SqlConnection, "GetQuestionnaireVersions",
                        Utility.DataAccess.BuildSqlParameterArray
                        ("@questionnaireID", SqlDbType.Int, Id));
                    if (ds.Tables.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            QuestionnaireVersion version = new QuestionnaireVersion(CachePolicy, row);
                            versions.Add(version);
                        }
                    }
                    else
                    {
                        throw new Exception("GetQuestionnaireVersions stored procedure returned incorrect results");
                    }
                }
                return versions;
            }
            set
            {
                versions = value;
                base.Versions = value;
            }
        }
    }
}
